package com.neusoft.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neusoft.base.BaseServiceImpl;
import com.neusoft.config.Pagination;
import com.neusoft.mapper.PackageMapper;
import com.neusoft.model.OrderSearch;
import com.neusoft.po.Package;
import com.neusoft.service.PackageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 收藏夹服务实现
 *
 * @author Lucifer
 * @since 2021/5/16 21:39
 */
@Service
public class PackageServiceImpl extends BaseServiceImpl<Package> implements PackageService {

    @Resource
    private PackageMapper packageMapper;

    @Override
    public BaseMapper<Package> getBaseDao() {
        return packageMapper;
    }

    @Override
    @Pagination
    public Object page(OrderSearch orderSearch) {
        return packageMapper.list(orderSearch.getId());
    }

    @Override
    public Integer save(Package entity) {
        Integer id = entity.getId();
        Package aPackage = packageMapper.selectOne(new QueryWrapper<Package>()
                .eq("item_id", entity.getItem_id())
                .eq("user_id", entity.getUser_id()));
        if (null != aPackage) {
            return aPackage.getId();
        }
        if (null == id) {
            insert(entity);
            id = entity.getId();
        } else {
            updateById(entity);
        }
        return id;
    }

    @Override
    public Integer remove(Integer[] ids, Integer user_id) {
        return packageMapper.delete(new QueryWrapper<Package>().in("item_id", ids).eq("user_id", user_id));
    }
}
